package com.adcc.swingui.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;

/**
 * <p>Title: JPopTextFiled</p>
 * <p>Description: 带浮动提示框的TextFiled</p>
 * <p>Copyright: Copyright (c) 2010</p>
 * @author jeff.zhao
 * @version 1.0.0
 */
public class JPopTextFiled extends JTextField
{
	private static final long serialVersionUID = 1L ;
	/**浮动框内信息框*/
	private JList contentList ;
	/**浮动框*/
	private JPopupMenu popframe ;
	/**是否显示浮动框*/
	private boolean showPopFrame = false ;
	/**待选择数据内容*/
	private List<String> popContentList ;
	private String currSelectedItem ;
	private boolean letterUpper ;
	/**事件查询事件*/
	private List<ActionListener> eventList = new ArrayList<ActionListener>() ;
	private int interBoxHeight = 200 ;
	
	public int getInterBoxHeight() 
	{
		return interBoxHeight;
	}

	public void setInterBoxHeight(int interBoxHeight) 
	{
		this.interBoxHeight = interBoxHeight;
	}

	public void addDataLoadEvent( ActionListener lis )
	{
		eventList.add( lis ) ;
	}

	public boolean isLetterUpper() 
	{
		return letterUpper;
	}

	public void setLetterUpper(boolean letterUpper) 
	{
		this.letterUpper = letterUpper;
	}

	public String getCurrSelectedItem() 
	{
		return currSelectedItem;
	}

	public void setCurrSelectedItem( String currSelectedItem ) 
	{
		this.currSelectedItem = currSelectedItem;
	}

	/**无参构造*/
	public JPopTextFiled()
	{
		this( null ) ;
	}
	
	/**
	 * 带初始数据构造
	 * */
	public JPopTextFiled( List<String> popContentList )
	{
		this.popContentList = popContentList ;
		this.addKeyListener( new TextKeyEventProcess() ) ;
		init() ;
	}
	
	public List<String> getPopContentList() 
	{
		return popContentList;
	}

	public void setPopContentList(List<String> popContentList) 
	{
		this.popContentList = popContentList;
	}
	
	/**初始化*/
	private void init()
	{
		DefaultListModel listModel = new DefaultListModel() ;
		this.contentList = new JList( listModel ) ;
		
		if( this.popContentList != null )
		{
			for( String contentStr : this.popContentList )
			{
				listModel.addElement( contentStr ) ;
			}
		}
		
		this.contentList.addMouseListener( new MouseAdapter() 
		{
			@Override
			public void mouseClicked(MouseEvent e) 
			{
				if( e.getClickCount() ==  2 && e.getButton() == MouseEvent.BUTTON1 )
				{
					doActionListBox( null) ;
				}
			}
		}) ;
		
		this.contentList.addKeyListener( new KeyAdapter() 
		{
			@Override
			public void keyReleased(KeyEvent e) 
			{
				if( e.getKeyCode() == KeyEvent.VK_ENTER )
				{
					doActionListBox( null ) ;
				}
			}
		}) ;
		
		this.popframe = new JPopupMenu() ;
		this.popframe.addPopupMenuListener( new PopupMenuListener() 
		{
			@Override
			public void popupMenuWillBecomeVisible(PopupMenuEvent e) 
			{
			}
			
			@Override
			public void popupMenuWillBecomeInvisible(PopupMenuEvent e) 
			{
			}
			
			@Override
			public void popupMenuCanceled(PopupMenuEvent e) 
			{
				setShowPopFrame( true ) ;
			}
		}) ;
		
		this.popframe.setLayout( new BorderLayout() ) ;
		this.popframe.add( new JScrollPane( contentList ) , BorderLayout.CENTER ) ;
	}
	
	public void fouseIndex(){
		
		int index = contentList.getSelectedIndex() ;
		
		if(index != -1){
			
			String value = (String) contentList.getSelectedValue() ;
			
			setCurrSelectedItem( value ) ;
			
			JPopTextFiled.this.setText( value );
			
		}
	}
	
	public void doActionIndex(){
		
		contentList.setSelectedIndex( 0 ) ;
	}
	
	
	private void doActionListBox( String selitem )
	{
		if( selitem == null || "".equals( selitem ))
		{
			if(contentList != null && contentList.getSelectedValue() != null )
			{
				setCurrSelectedItem( contentList.getSelectedValue().toString() ) ;
			}
		}
		else
		{
			setCurrSelectedItem( selitem ) ;
		}
		
		if( getActionListeners() != null )
		{
			for( int i = 0 ; i < getActionListeners().length ;  i ++ )
			{
				ActionEvent event = new ActionEvent( JPopTextFiled.this ,i, "" ) ;
				getActionListeners()[i].actionPerformed( event ) ;
			}
		}
		
		setShowPopFrame( false ) ;
		setText( getCurrSelectedItem() ) ;
	}

	public boolean isShowPopFrame() 
	{
		return showPopFrame;
	}
	
	private void processDataByKey( String key )
	{
		if( this.popContentList != null  )
		{
			DefaultListModel model1 = new DefaultListModel();
			for( String str : popContentList )
			{
				if( key == null || "".equals( key ))
				{
					model1.addElement( str ) ;
					continue ;
				}
				
				if( str.contains( key ))
				{
					model1.addElement( str ) ;
				}
			}
			contentList.setModel(model1);
		}
	}

	/**设置浮动框是否显示*/
	public void setShowPopFrame( boolean showPopFrame ) 
	{
		this.showPopFrame = showPopFrame ;
		
		if( !showPopFrame )
		{
			popframe.setVisible( false ) ;
		}
		else
		{
			popframe.setPopupSize( new Dimension( this.getWidth() ,  this.interBoxHeight ) ) ;
			popframe.show( this.getParent() , this.getX()   , this.getY() + this.getHeight() ) ;
			this.requestFocus() ;
		}
	}
	
	private class TextKeyEventProcess extends KeyAdapter
	{
		@Override
		public void keyReleased(KeyEvent e) 
		{
			if( e.getKeyCode() == KeyEvent.VK_ENTER && getText() != null && ! "".equals( getText() ))
			{
				int index = contentList.getSelectedIndex() ;
				
				if(index != -1){
					
					String value = (String) contentList.getSelectedValue() ;
					
					setCurrSelectedItem( value ) ;
					
					JPopTextFiled.this.setText( value );
					
				}else{
					
					doActionListBox( getText() ) ;
				}
			
				setShowPopFrame( false ) ;
				
				return ;
			}
			else if( e.getKeyCode() == KeyEvent.VK_ESCAPE )
			{
				setShowPopFrame( false ) ;
				return ;
			}
			
			if( isLetterUpper() )
			{
				toUpper( JPopTextFiled.this , e ) ;
			}
			
			for( ActionListener lis : eventList )
			{
				lis.actionPerformed( null ) ;
			}
			
			reflashPopList() ;
			
			if(  e.getKeyCode() == KeyEvent.VK_DOWN && contentList.getModel().getSize() > 0 )
			{
				contentList.setSelectedIndex( 0 ) ;
				contentList.requestFocus() ;
			}
		}
	}
	
	private String toUpper( JTextField text , KeyEvent e )
	{
		StringBuffer newSb = new StringBuffer() ;
		
		for( char ch : text.getText().toCharArray() ) 
		{
			if( Character.isLetter( ch ) )
			{
				newSb.append( Character.toUpperCase( ch  ) ) ;
			}
			else 
			{
				newSb.append( ch ) ;
			}
		}
	
		text.setText( newSb.toString() ) ;
		
		return newSb.toString();
	}

	private void reflashPopList()
	{
		setShowPopFrame( true ) ;
		processDataByKey( this.getText() ) ;
		this.contentList.setSelectedIndex(0) ;
	}
	
	public static void main( String[] args )
	{
		List<String> data = new ArrayList<String>() ;
		data.add( "jacky" ) ;
		data.add( "aladdinty" ) ;
		data.add( "nihao" ) ;
		data.add( "haha" ) ;
		data.add( "ppwang" ) ;
		data.add( "xiaotiantia" ) ;
		data.add( "jacky" ) ;
		data.add( "aladdinty" ) ;
		data.add( "nihao" ) ;
		data.add( "haha" ) ;
		data.add( "ppwang" ) ;
		data.add( "xiaotiantia" ) ;
		data.add( "jacky" ) ;
		data.add( "aladdinty" ) ;
		data.add( "nihao" ) ;
		data.add( "haha" ) ;
		data.add( "ppwang" ) ;
		data.add( "xiaotiantia" ) ;
		
		JPopTextFiled file = new JPopTextFiled( data ) ;
		file.setInterBoxHeight( 100 ) ;
		file.addActionListener( new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println( ((JPopTextFiled)e.getSource()).getCurrSelectedItem() ) ;
			}
		}) ;
		JFrame jf = new JFrame() ;
		jf.setBounds( 100 , 100 , 500, 600) ;
		jf.setLayout( new FlowLayout()  ) ;
		file.setPreferredSize( new Dimension(300 , 20 )) ;
		jf.getContentPane().add( new JLabel( "aladdinty:") ) ;
		jf.getContentPane().add( file ) ;
		jf.setVisible( true ) ;
		
	}
}
